package com.guigu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.ProductMapper;
import com.guigu.mapper.SupplierMapper;
import com.guigu.mapper.SupplierProductMapper;
import com.guigu.pojo.Product;
import com.guigu.pojo.Supplier;
import com.guigu.pojo.SupplierProduct;
import com.guigu.service.SupplierProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 供货商商品关联表 服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@Service
public class SupplierProductServiceImpl extends ServiceImpl<SupplierProductMapper, SupplierProduct> implements SupplierProductService {

    @Autowired
    private SupplierProductMapper supplierProductMapper;

    @Override
    public Page<SupplierProduct> getSupplierProductServiceByProductId(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }
        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }

        Page<SupplierProduct> page = new Page<>(currentPage, pageSize);
        // 确保map不为null
        if (map == null) {
            map = new HashMap<>();
        }

        List<SupplierProduct> supplierProducts = supplierProductMapper.getSupplierProducts(map);
        System.out.println(supplierProducts);
        if (supplierProducts != null) {
            page.setRecords(supplierProducts);
            return page;
        }
        return null;
    }
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private SupplierMapper supplierMapper;


    @Override
    public Page<SupplierProduct> getSupplierProductBySupplierId(Integer supplierId,Integer currentPage,Integer pageSize ) {

        Page<SupplierProduct> page = new Page<>(currentPage, pageSize);
        QueryWrapper<SupplierProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplier_id",supplierId);
        Page<SupplierProduct> supplierProductPage = supplierProductMapper.selectPage(page, queryWrapper);
        for(SupplierProduct supplierProduct :supplierProductPage.getRecords()){
            Integer productId = supplierProduct.getProductId();
            Integer supplierId1 = supplierProduct.getSupplierId();

            Product product = productMapper.selectById(productId);
            supplierProduct.setProduct(product);
            Supplier supplier = supplierMapper.selectById(supplierId1);
            supplierProduct.setSupplier(supplier);
        }
        return supplierProductPage;
    }
}
